from rest_framework.viewsets import ModelViewSet
from aaa import models
from rest_framework import serializers
from rest_framework.response import Response
import json
from django.http import JsonResponse
from aaa.utils.pager import myPageNumberPagination

class reportSerializer(serializers.ModelSerializer):
    incidence = serializers.CharField(source='get_incidence_display')
    equipment_status = serializers.CharField(source='get_equipment_status_display')
    class Meta:
        model = models.report1170
        fields = '__all__'

class reportSearchView(ModelViewSet):
    queryset = models.report1170.objects.all()
    serializer_class = reportSerializer
    authentication_classes = []

    def list(self, request, *args, **kwargs):
        print('args',request._request.GET.get('pagenum'))
        devices = self.get_queryset()
        # pg = myPageNumberPagination()
        # xx = pg.paginate_queryset(queryset=self.queryset,request=request,view=self)
        # ser = yySerializer(instance=xx,many=True)
        ser = reportSerializer(instance=devices,many=True)
        ret = {}
        ret['data'] = json.dumps(ser.data)
        ret['total'] = self.queryset.count()
        return JsonResponse(ret)

    # def update(self, request, *args, **kwargs):
    #     print('updateDevices res', request.data)
    #     data = request.data
    #     state = data['incidence']
    #     equipment_state = data['equipment_status']
    #     solve_date = data['solve_date']
    #     print('state000', type(state),state)
    #     if state == '1' or state == '红色预警':
    #         state = 1
    #     elif state == '2' or state == '橙色预警':
    #         state = 2
    #     elif state == '3' or state == '黄色预警':
    #         state = 3
    #     elif state == '4' or state == '蓝色预警':
    #         state = 4
    #     print('state111', state)
    #     try:
    #         obj = models.yy1170.objects.filter(index=data['index']).update(
    #             describle=data['describle'], proposer=data['proposer'], question_state=data['question_state'],
    #             incidence=state, responsible_person=data['responsible_person'], deal=data['deal'],maintenance_date=data['maintenance_date'],
    #             solve_date=solve_date,offline_date=data['offline_date'],equipment_status=equipment_state,pics=data['pics'])
    #         return JsonResponse({'meta': {'status': 201, 'msg': 'ok'}})
    #     except Exception as e:
    #         print('e',e)